package com.semp.gu.codec;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractCodec implements ICodec {

	private File				workingFolder;
	private final static Logger	logger	= LoggerFactory.getLogger(AbstractCodec.class);

	public AbstractCodec(String type) {
		/*
		 * Creating tmp dir
		 */
		String userHome = System.getProperty("user.home");
		File rootFolder = new File(userHome + File.separator + ".gl");
		if (!rootFolder.exists()) {
			rootFolder.mkdir();
			logger.debug("Created {}", rootFolder);
		}
		workingFolder = new File(rootFolder.getAbsolutePath() + File.separator + type + "-" + new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(new Date()));
		if (!workingFolder.exists()) {
			workingFolder.mkdir();
			logger.debug("Created {}", workingFolder);
		}
	}

	public File getWorkingFolder() {
		return workingFolder;
	}

	
	public void cleanUp() {
		logger.debug("Deleting working folder {}", workingFolder);
		try {
			workingFolder.delete();
		} catch (Throwable e) {
			logger.error("", e);
		}
	}
}
